package com.example.qinghe.frontController.frontmeeting;


import cn.hutool.core.io.FileUtil;
import com.example.qinghe.entity.meeting.Meeting;
import com.example.qinghe.entity.meeting.Meetingroom;
import com.example.qinghe.service.meeting.MeetingService;
import com.example.qinghe.service.meeting.MeetingroomService;
import com.example.qinghe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("/front/room")
@Api(tags = "会议系统->会议室管理", description = "FrontMeetingroomController")
public class FrontMeetingroomController {

    @Autowired
    private MeetingService meetingService;

    @Autowired
    private MeetingroomService meetingroomService;

    @Autowired
    private MeetingroomService service;


    @ApiOperation("根据会议室编号，查找此会议室已建立的会议信息,包含会议室信息")
    @GetMapping("/meetingCountByRoomId/{id}")
    public Result meetiCountList(@PathVariable("id")Long id){
        List<Object> objectList=new ArrayList<>();
        List<Meeting> meetings = meetingService.selectByRoomId(id);
        for (Meeting mt: meetings) {
            Long id1 = mt.getId();
            Meeting mtg = meetingService.getMeetingIncludeRoomById(id1);
            objectList.add(mtg);
        }

        System.out.println(">>>>>>>>>>"+objectList);
//        for (Meeting meeting : meetings) {
//            objectList.add(meeting);
//            Long id1 = meeting.getMeetingroomId();
//            Meetingroom meetingroom = meetingroomService.SelectMeetingroomById(id1);
//            objectList.add(meetingroom);
//            objectList.add(";");
//        }
        if (objectList==null) {
            return Result.error("没有找到");
        }

        return Result.success(objectList);
    }

    @ApiOperation("查找所有会议室的信息，会议地址提供对应地点的经纬度")
    @GetMapping("/list")
    public Result meetigroomList(){
        List<Meetingroom> meetingrooms = service.SelectAllMeetingroom();
        return Result.success(meetingrooms);
    }

    @ApiOperation("查找单个会议室的信息，会议地址提供对应地点的经纬度")
    @GetMapping("/search/{id}")
    public Result SelectMeetingroomById(@PathVariable("id")Long id){
        Meetingroom meetingroom = service.SelectMeetingroomById(id);
        return Result.success(meetingroom);
    }

    @ApiOperation("会议室添加，会议地址提供对应地点的经纬度")
    @PostMapping("/add")
    public Result addMeetingroom(@RequestBody Meetingroom meetingroom){
        int meetroom = service.addMeetingroom(meetingroom);
        if (meetroom == 0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }

    @ApiOperation("会议室信息修改")
    @RequestMapping(value = "/edit" ,method = RequestMethod.PUT)
    public Result editMeetingroomById(@RequestBody Meetingroom meetingroom){
        int meetroom = service.editMeetingroom(meetingroom);
        if (meetroom == 0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }

    @ApiOperation("删除单个会议室")
    @GetMapping("/del/{id}")
    public Result delMeetingroom(@PathVariable("id")Long id){
        int del = service.delMeetingroomById(id);
        if (del == 0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    //上传会议室照片
    @Value("${upload-path}")
    private String path;

    @ApiOperation("会议室照片上传")
    @PostMapping("/upload")
    public Result upload2(@RequestParam(name="file") MultipartFile file){
        Map<String,Object> m=new HashMap<>();
        if (file!=null &&!file.isEmpty()) {
            File f = new File(path);
            if (!f.exists())
                f.mkdirs();
            //原来的文件名
            String filename = file.getOriginalFilename();
            String houzhui = filename.substring(filename.lastIndexOf('.'));
            String newName = UUID.randomUUID() + houzhui;

            try {
                file.transferTo(new File(f, newName)); // 本地存储 d:temp/UUID.randomUUID() + houzhui
                m.put("code",0);
                //String url1=path+File.separator+newName;   //本地路径 d:temp/UUID.randomUUID() + houzhui
                return Result.success("img/" + newName,"上传成功");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        m.put("code",415);
        m.put("msg","图片没有选择");
        return Result.error(415,"图片没有选择");
    }


    /**
     * 会议室照片预览
     * @param fileName
     * @param response
     * @throws IOException
     */
    @GetMapping("/download/{fileName}")
    public void download(@PathVariable String fileName, HttpServletResponse response) throws IOException {
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));  // 附件下载
        String ROOT_PATH = "img" + File.separator + path;
        String filePath = ROOT_PATH  + File.separator + fileName;
        if (!FileUtil.exist(filePath)) {
            return;
        }
        byte[] bytes = FileUtil.readBytes(filePath);
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);  // 数组是一个字节数组，也就是文件的字节流数组
        outputStream.flush();
        outputStream.close();
    }


}
